# 自定义分页 组件
import copy
from django.http.request import QueryDict
from django.utils.safestring import mark_safe
class Pagenations(object):
    def __init__(self, request, queryset, page_size=10, page_params='page', plus = 5):

        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict = query_dict
        page = request.GET.get(page_params, "1")
        if page.isdecimal():
            page = int(page)
        else:
            page = 1
        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size

        self.page_queryset = queryset[self.start:self.end]

        total_count = queryset.count()
        # 总页码
        total_page_count, div = divmod(total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count
        self.plus = plus
        self.page_params = page_params

    def html(self):
        # 计算出当前页的 前5页和后5页
        if self.total_page_count <= 2 * self.plus + 1:
            start_page = 1
            end_page = self.total_page_count
        else:
            # 数据库的数据 > 11

            # 当前页<5时
            if self.page <= self.plus:
                start_page = 1
                end_page = 2 * self.plus + 1
            else:
                # 当前页 > 5
                if (self.page + self.plus) > self.total_page_count:
                    start_page = self.total_page_count - 2 * self.plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus

        # 页码
        page_str_list = []

        # self.query_dict.setlist(self.page_params, [1])
        # self.query_dict.urlencode()



        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.page_params, [self.page - 1])

            prev = '<li class="page-item prev "><a class="page-link" href="?{}"><i class="tf-icon bx bx-chevron-left"></i></a></li>'.format(
                self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_params, [1])
            prev = '<li class="page-item prev "><a class="page-link" href="?{}"><i class="tf-icon bx bx-chevron-left"></i></a></li>'.format(
                self.query_dict.urlencode())
        page_str_list.append(prev)

        for i in range(start_page, end_page + 1):
            if i == self.page:
                self.query_dict.setlist(self.page_params, [i])
                ele = '<li class="page-item active"><a class="page-link" href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            else:
                self.query_dict.setlist(self.page_params, [i])
                ele = '<li class="page-item"><a class="page-link" href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            page_str_list.append(ele)

        # 下一页
        if self.page < self.total_page_count:
            self.query_dict.setlist(self.page_params, [self.page + 1])
            prev = '<li class="page-item next "><a class="page-link" href="?{}"><i class="tf-icon bx bx-chevron-right"></i></a></li>'.format(
                self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_params, [self.total_page_count])
            prev = '<li class="page-item next "><a class="page-link" href="?{}"><i class="tf-icon bx bx-chevron-right"></i></a></li>'.format(
                self.query_dict.urlencode())
        page_str_list.append(prev)

        page_string = mark_safe(''.join(page_str_list))
        return page_string